﻿#include "WrapperDataObject.h"
// core
#include "FITK_Kernel/FITKCore/FITKDataRepo.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
//framework
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
//python

#include <PythonQt.h>
#include <PythonQt_QtAll.h>
#include <PythonQtScriptingConsole.h>
#include "FITK_Kernel/FITKPython/FITKPythonInterface.h"
//operator
#include "OperatorsInterface/GraphEventOperator.h"
#include "FITK_Kernel/FITKCore/FITKActionOperator.h"
#include "OperatorsInterface/GraphInteractionOperator.h"
#include "OperatorsInterface/TreeEventOperator.h"
//interface
#include "FITK_Interface/FITKInterfaceGeometry/FITKGeoCommandList.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKGeoInterfaceFactory.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeoModelBox.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeoModelSphere.h"
//component
#include "FITK_Component/FITKGeoCompOCC/FITKOCCModelSolid.h"
#include "FITK_Component/FITKGeoCompOCC/FITKOCCModelOperSolid.h"
#include <QMetaEnum>
#include <QDebug>


bool WrapperDataObject::update()
{
    if (!m_command) return false;
    bool flag = m_command->update();
    return flag;
}

WrapperDataObject::~WrapperDataObject()
{
    if (!m_command)
    {
        delete m_command;
        m_command = nullptr;
    }
}

Interface::FITKAbsGeoCommand * WrapperDataObject::getCommand()
{
    return m_command;
}


QString WrapperDataObject::getName()
{
    if (m_command) return m_command->getDataObjectName();
    return QString();
}

void WrapperDataObject::setName(const QString & name)
{
    if (!m_command) return;
    m_command->setDataObjectName(name);
}

WrapperDataObjectWrapper::WrapperDataObjectWrapper()
{
    geoCmdUndoList = FITKGLODATA->getGeometryData<Interface::FITKGeoCommandList>();
    geoCmdRedoList = geoCmdUndoList->getUndoList();
}

QString WrapperDataObjectWrapper::getName(WrapperDataObject* obj)
{
    if (obj) return obj->getName();
    return QString();
}

void WrapperDataObjectWrapper::registerSig(WrapperDataObject * obj, int sigObjID, const QString & funcName)
{
    auto geoData = geoCmdUndoList->getDataByID(sigObjID);
    if (!geoData) return;

    auto mainModel = PythonQt::self()->getMainModule();
    auto modelObject = PythonQt::self()->lookupObject(mainModel, obj->getClassName());
    PythonQt::self()->addSignalHandler(this, SIGNAL(destoried(WrapperDataObject*)), modelObject.object(), funcName);

    connect(geoData, &Interface::FITKAbsGeoCommand::dataObjectDestoried, this, [=] {
        emit destoried(obj);
    });
}

void WrapperDataObjectWrapper::updateUI(int caseid)
{
    // 刷新树。
    auto treeOper = FITKOPERREPO->getOperatorT<EventOper::TreeEventOperator>("TreeEvent");
    treeOper->updateGeometryTree();

    // 刷新可视化窗口。
    auto operGraph = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("GraphPreprocess");
    if (operGraph) operGraph->updateGraphRecursively(caseid, 2, false);

    // 初始化前处理拾取功能操作器。
    auto m_GraphPickOper = FITKOPERREPO->getOperatorT<EventOper::GraphInteractionOperator>("GraphPick");
    m_GraphPickOper->setCustomPickMode();
}
